$(document).ready(function() {

//    Start Choosing only one checkboxes in catalog
    var radioInt = $(".radio-int");
    var radioTv = $(".radio-tv");
    var radioPhone = $(".radio-phone");

    loopCheckers(radioInt);
    loopCheckers(radioPhone);
    loopCheckers(radioTv);

    function loopCheckers(obj) {
        $(obj).each(function() {
            $(this).mousedown(function() {
                var className = "." + $(this).attr("class") + ":checked";
                if (!$(this).is(":checked")) {
                    $(className).attr("checked", false);
                }
            });
        });
    }
//    End Choosing only one checkboxes in catalog

//Start Create component group
    var compInput = $("#comp-in-group");

    function takeSelected() {
        var selected = $("#components input[type=checkbox]:checked");
        var selText = "";
        var selId = "";
        selected.each(function() {
            if (selText != "") {
                selText += ", ";
                selId += ";";
            }
            selText += $(this).val();
            selId += $(this).attr("id");
        });
        compInput.text(selText);
        compInput.attr("compid", selId);
    }
//Stop Create component group


//     Start Show-hide fly-product detail
    var viewProduct = $(".view-det-btn");
    var flyBlock = $("#fly-block");
    var closeBtn = $("#close");
    var saveBtn = $("#save-btn");
    var prodBlock = $("#product-block");

    closeBtn.click(function() {
        closeFlyBlock();
    });
    saveBtn.click(function() {
        closeFlyBlock();
    });

    viewProduct.each(function() {
        $(this).css({
            "font-size": "12px",
            "-moz-border-radius": "15px",
            "-webkit-border-radius": "15px",
            "-ms-border-radius": "15px",
            "-o-border-radius": "15px",
            "border-radius": "15px",
        });
        $(this).click(function() {
            showFlyBlock();
        });
    });

    function closeFlyBlock() {
        flyBlock.hide();
        prodBlock.fadeTo(0, 0);
    }
    function showFlyBlock() {
        flyBlock.show();
        prodBlock.fadeTo(200, 1);
    }
//     Stop Show-close fly-product detail

//     Start Grouping components
    var chkCompGroup = $(".component-group");

    chkCompGroup.each(function() {
        $(this).change(function() {
            if ($(this).is(":checked")) {
                var value = $(this).val();
                var compIdArr = value.split(";");
                var comp;
                for (var i in compIdArr) {
                    comp = $("#fly-block #" + compIdArr[i]);
                    if (!comp.is(":checked"))
                        comp.click();
                }
            }
        });
    });

    $("#components input[type=checkbox]").each(function() {
        $(this).change(function() {
            if ($(this).is(":checked")) {
                $(this).attr("checked", true);
            } else {
                $(this).attr("checked", false);
                checkGroup($(this).attr("id"));
            }
            takeSelected();
        });
    });

    function checkGroup(id) {
        chkCompGroup.each(function() {
            var value = $(this).val();
            var compIdArr = value.split(";");
            var comp;
            for (var i in compIdArr) {
                if (id == compIdArr[i])
                    $(this).attr("checked", false);
            }
        });
    }
//     Stop Grouping components


});


